﻿using System.Text.RegularExpressions;
using com.fukefu.source;
using com.fukefu.tools;

namespace com.fukefu.info.b2b.auto
{
    public class EBDoor : Information
    {
        public override bool Login(string name, string pass, string vcode)
        {
            string html = source.Get("http://www.ebdoor.com/Member/UserLoginInfo.aspx?2037790373");
            html = source.Get("http://www.ebdoor.com/member/login.aspx");
            
            ParametersData pd = new ParametersData();
            pd.Add("__EVENTTARGET", "LinkButton_Submit");
            pd.Add("__EVENTARGUMENT", "");
            pd.Add("__VIEWSTATE", Regex.Match(html, "id=\"__VIEWSTATE\" value=\"([^\"]+)\"").Groups[1].Value);
            pd.Add("TextBox_LoginAccount", name);
            pd.Add("TextBox_LoginPassword", pass);
            pd.Add("Hidden_Valid", Regex.Match(html, "id=\"Hidden_Valid\" value=\"([^\"]+)\"").Groups[1].Value);
            pd.Add("TextBox_ValidateCode", vcode);

            html = source.Post("http://www.ebdoor.com/member/login.aspx", pd.Read(), "http://www.ebdoor.com/member/login.aspx");
            if (html.IndexOf("window.document.location.href = 'index.aspx'") != -1)
            {
                return true;
            }
            return false;
        }

        public override InformationResult PostArticle(string title, string content, string vcode)
        {
            InformationResult result = new InformationResult();
            string html = "";
            SourceFormData sfd = new SourceFormData("utf-8");
            //头部数据
            sfd.Write("__EVENTTARGET", "ctl00$ContentPlaceHolder1$LinkButton_OK");
            sfd.Write("__EVENTARGUMENT", "");
            sfd.Write("__VIEWSTATE", Regex.Match(html, "id=\"__VIEWSTATE\" value=\"([^\"]+)").Groups[1].Value);
            sfd.Write("ctl00$ContentPlaceHolder1$Text_ProductCode", "");
            sfd.Write("ctl00$ContentPlaceHolder1$Text_ProductName", title);
            sfd.Write("ctl00$ContentPlaceHolder1$Text_CatalogName", site.TypeId[0]);
            sfd.Write("ctl00$ContentPlaceHolder1$Text_CatalogId", site.TypeId[1]);
            sfd.Write("ctl00$ContentPlaceHolder1$DropDownList_ProdCusType", "");
            sfd.Write("ctl00$ContentPlaceHolder1$Text_TradeMark", pif.Get("品牌"));
            sfd.Write("ctl00$ContentPlaceHolder1$Text_ProductModel", pif.Get("品牌"));
            sfd.Write("ctl00$ContentPlaceHolder1$Text_ProduceArea", pif.Get("产地"));
            sfd.Write("Text_ProductDesc", content);
            //发送文件
            sfd.WriteFile("ctl00$ContentPlaceHolder1$File1", RandomPicture.Get());
            sfd.WriteFile("ctl00$ContentPlaceHolder1$File2", RandomPicture.Get());
            sfd.WriteFile("ctl00$ContentPlaceHolder1$File3", RandomPicture.Get());
            //尾部数据
            sfd.Write("ctl00$ContentPlaceHolder1$Text_DeliverPrice1", "");
            sfd.Write("ctl00$ContentPlaceHolder1$CheckBox_2", "on");
            sfd.Write("ctl00$ContentPlaceHolder1$Text_DeliverPrice2", "10.00");
            sfd.Write("ctl00$ContentPlaceHolder1$Text_DeliverPrice3", "");
            sfd.Write("ctl00$ContentPlaceHolder1$Text_DeliverPrice4", "");
            sfd.Write("ctl00$ContentPlaceHolder1$NewData", "2012-05-02");
            sfd.Write("ctl00$ContentPlaceHolder1$Text_ProductPrice", pif.Get("价格"));
            sfd.Write("ctl00$ContentPlaceHolder1$Text_SupplyQuantity", pif.Get("库存数量"));
            //结尾
            html = source.Post("http://www.ebdoor.com/Product/CreateProduct.aspx", sfd.GetContentType(), sfd.Read(), "http://www.ebdoor.com/Product/CreateProduct.aspx");
            if (html.IndexOf("信息已提交,我们将尽快审核") != -1)
            {
                MyFunction.Sleep(90);
                html = source.Get("http://www.ebdoor.com/Product/ProductsOnSale.aspx");
                result.Code = 1;
                result.Content = "http://product.ebdoor.com/Products/" + Regex.Match(html, "Products/([0-9]+)\\.aspx\"\\s+target=\"_blank\" id=\"Name_0\">\\s+" + title).Groups[1].Value + ".aspx";
            }
            else if (Regex.IsMatch(html, "t/javascript\">//<!\\[CDATA\\[alert\\('([^']+)"))
            {
                result.Content = Regex.Match(html, "t/javascript\">//<!\\[CDATA\\[alert\\('([^']+)").Groups[1].Value;
            }
            return result;
        }
        public override System.Drawing.Image GetFlushCodeImage()
        {
            return source.GetImage("http://www.ebdoor.com/Resource/ValidateCode/ValidateCode.aspx?" + random.Next(100000));
        }
    }
}
